package com.hl.sfzy.controller.wxwappController;

import com.github.pagehelper.PageInfo;
import com.hl.sfzy.entity.wxwapp.Heartmark;
import com.hl.sfzy.service.wxwappService.WXWHeartMarkService;
import com.hl.sfzy.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.UUID;

/**
 * @className: Heartmark
 * @author: WXW
 * @date: 2024/9/10 10:35
 * @Version: 1.0
 * @description:
 */
@RestController
@RequestMapping("/heartmark")
public class HeartMarkController {
    @Autowired
    private WXWHeartMarkService WXWHeartMarkService;

    @GetMapping("/findAllHeartMark")
    public R findAllHeartMark(Integer page, Integer size) {
        PageInfo<Heartmark> heartMarks = WXWHeartMarkService.findAllHeartMark(page, size);
        return R.success(heartMarks);
    }

    //查询user表的信息
    @GetMapping("/selectAllUserById")
    public R selectAllUserById(){
        return R.success(WXWHeartMarkService.findUserInfo());
    }

    //删除
    @DeleteMapping("/deleteHeartMarkById/{id}")
    public R deleteHeartMarkById(@PathVariable("id") Integer id){
        return WXWHeartMarkService.deleteHeartMark(id);
    }


    //添加
    @PostMapping("/addHeartMark")
    public R addHeartMark(@RequestBody Heartmark heartmark){
        System.out.println("heartmark = " + heartmark);
        return R.success(WXWHeartMarkService.addHeartMark(heartmark));
    }

    //上传文件
    @PostMapping("/fileup")
    public R<String> upload(MultipartFile file) throws IOException {
        String path = "D:\\upload\\";
        File file1 = new File(path);
        if (!file1.exists()){
            file1.mkdir();
        }
        String originalFilename = file.getOriginalFilename();
        String ext = originalFilename.substring(originalFilename.lastIndexOf("."));
        String filename = UUID.randomUUID().toString() + ext;
        path = path + filename;
        file.transferTo(new File(path));
        return R.success(filename);
    }
    //通过网址查看图片
    @GetMapping("/downImg")
    public void down(String file, HttpServletResponse resp) throws Exception {
        String path = "d:\\upload\\" + file;
        FileInputStream inputStream = new FileInputStream(path);
        ServletOutputStream outputStream = resp.getOutputStream();

        byte[] bytes = new byte[1024];
        int i = inputStream.read(bytes);
        while (i != -1) {
            outputStream.write(bytes, 0, i);
            i = inputStream.read(bytes);
        }
        outputStream.close();
        inputStream.close();
    }


    //根据id查询（修改）
    @GetMapping("/findHeartMarkById")
    public R findHeartMarkById(Integer id){
        return R.success(WXWHeartMarkService.findHeartMarkById(id));
    }

    //修改
    @PutMapping("/updateHeartMark")
    public R updateHeartMark(@RequestBody Heartmark heartmark){
        return WXWHeartMarkService.updateHeartMark(heartmark);
    }

    //根据测评名模糊查询
    @GetMapping("/findHeartMarkByName")
    public R findHeartMarkByName(String name,Integer page, Integer size){
        return WXWHeartMarkService.findHeartMarkByName(name,page,size);
    }

    //app端心理知识列表跳转详情页
    @GetMapping("/findHeartMarkByIdForApp")
    public R findHeartMarkByIdForApp(Integer id){
        return WXWHeartMarkService.findHeartMarkByIdApp(id);
    }

}
